import axios from 'axios'
let http=axios.create({
    // baseURL:'http://localhost:8080'
})
http.interceptors.request.use(
    (config) =>{
        if(window.localStorage.getItem('token')){
            //如果有token，携带token请求头进行传递数据
            config.headers.Authorization = window.localStorage.getItem('token');
            }
            return config ;
    },(error)=>{
        return Promise.reject(error);
    }
)

http.interceptors.response.use(res => {
    return Promise.resolve(res.data)
}, err => {
    switch(err.response.status){
    case 401:
        console.log('未登录')
        window.location.href='#/login'
        break
    default:
        console.log('其他错误')
        break
    }
    console.log(err.response);
    return Promise.reject(err)
})

export default http;